import {md5} from "../utils/md5";

export interface ISecureFactory {
    encode(header: object, params: object);
}

export class CommonSecureFactory implements ISecureFactory {
    encode(header: object, params: object) {
        if (header == null || params == null) return;

        let str = "";
        ["app", "version", "ts", "Authorization", "debug"].forEach(key => {
            str += `${key}=${header[key]}`;
        });
        const keys = Object.keys(params);
        keys.sort((a, b) => {
            return a.localeCompare(b);
        });
        keys.forEach(key => {
            str += `${key}=${encodeURIComponent(params[key])}`;
        });
        str += API._secretKey;
        header["sec"] = md5(str);
        cc.log("加密结果:", header["sec"], str);
    }
}

export class AlphaSecureFactory implements ISecureFactory {
    encode(header: object, params: object) {
    }
}

export const API = {
    // 客户端与服务器保持安全通信的密钥.
    _secretKey: "fGud5Oq%C%ZA6Q%3ulFGB32@b8wTO&",
    client: "app",
    authUrl: false ? "http://localhost:8082/micro-server-v2" : "https://sanrioapi.kehaisheng.com/micro-server-v3/",
    baseUrl: false ? "http://localhost:8082/micro-server-v2" : "https://sanrioapi.kehaisheng.com/micro-server-v3/",
    app: "dnf2",
    version: "1",
    debug: 1,
    secure: false ? new AlphaSecureFactory() : new CommonSecureFactory()
};

